package com.itheima.controller.pms;

import com.itheima.api.pms.PmsBrandService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.PmsBrand;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    @DubboReference
    private PmsBrandService pmsBrandService;          //远程调用

/*
* 添加品牌
* */
    @PostMapping("/create")
    public AjaxResult  create (@RequestBody PmsBrand pmsBrand){
        pmsBrandService.save(pmsBrand);
        return AjaxResult.success("操作成功");
    }

    /*
    * 批量删除品牌
    * */
    @PostMapping("/delete/batch")
    public AjaxResult  deletes(@RequestParam List<Long> ids){
        pmsBrandService.removeByIds(ids);
        return AjaxResult.success("操作成功");
    }

    /*
    * 删除品牌
    * */
    @GetMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        pmsBrandService.removeById(id);
        return AjaxResult.success("操作成功");
    }

    /*
    * 根据品牌名称分页获取品牌列表
    * */
    @GetMapping("/list")
    public   AjaxResult  page(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(required = false) String keyword){
        TableDataInfo tableDataInfo=pmsBrandService.queryPage(pageNum,pageSize,keyword);
        return AjaxResult.success(tableDataInfo);
    }

    /*
    * 获取全部品牌列表
    * */
    @GetMapping("/listAll")
    public AjaxResult list(){
        List<PmsBrand> list = pmsBrandService.list();
        return AjaxResult.success(list);
    }

    /*
    * 批量更新厂家制造商状态
    * */
    @PostMapping("/update/factoryStatus")
    public  AjaxResult updateFactoryStatus(@RequestParam Integer factoryStatus,int[] ids ){
        List<PmsBrand> pmsBrands = new ArrayList<>();
        for (int id : ids) {
            pmsBrands.add(pmsBrandService.getById(id));
        }
        pmsBrands.forEach(pmsBrand -> pmsBrand.setFactoryStatus(factoryStatus));
        pmsBrandService.updateBatchById(pmsBrands);
        return AjaxResult.success("操作成功");
    }

    /*
    * 批量更新显示状态
    * */
    @PostMapping("/update/showStatus")
    public AjaxResult update(@RequestParam int [] ids,Integer showStatus){
        ArrayList<PmsBrand> pmsBrands = new ArrayList<>();
        for (int id : ids) {
            PmsBrand brand = pmsBrandService.getById(id);
            pmsBrands.add(brand);
        }
        pmsBrands.forEach(pmsBrand -> {
            pmsBrand.setShowStatus(showStatus);
        });
        pmsBrandService.updateBatchById(pmsBrands);
        return AjaxResult.success("操作成功");
    }

    /*
    * 更新品牌
    * */
    @PostMapping("/update/{id}")
    public  AjaxResult updateById(@PathVariable Integer id,@RequestBody PmsBrand pmsBrand){
        pmsBrand.setId(id);
        pmsBrandService.updateById(pmsBrand);
        return AjaxResult.success("操作成功");
    }

/*
* 根据编号查询品牌信息
* */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Integer id){
        PmsBrand brand = pmsBrandService.getById(id);
        return AjaxResult.success(brand);
    }
}
